package mainWindowSupport;

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.ImageIcon;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import javax.swing.JFrame;
import menuMaster.MenuBuilder;
import palette.*;
import actionHandlers.CloseHandler;
import utilities.Logging;

public class MainWindow extends JFrame {

	private static final long serialVersionUID = -2303006344184845201L;

	public MainWindow()
	{
		Logging.write(0, "test");
		this.setTitle("QP: A Shape Drawer");
		setIconImage(new ImageIcon("qp_fade.jpg").getImage());
		//MenuBar topMenu = new MenuBar();
		menuMaster.MenuBuilder topMenuBldr = new menuMaster.MenuBuilder("menubar.xml", MenuBuilder.BUILDMENUBAR);
		javax.swing.JMenuBar topMenu = topMenuBldr.getMenuBar();

		//Lay out the label and scroll pane from top to bottom.
		CanvasManager canvas = CanvasManager.getInst();  //added JD
		CanvasManager.getInst().addNewCanvas();
		
		//ToolBar mainBar = ToolBar.getInst();
		menuMaster.MenuBuilder mainBarBldr = new menuMaster.MenuBuilder("toolbar.xml", MenuBuilder.BUILDTOOLBAR);
		javax.swing.JToolBar mainBar = mainBarBldr.getToolBar();
		
		//Put everything together, using the content pane's BorderLayout.
		Container contentPane = getContentPane();
		//contentPane.add(scroll, BorderLayout.CENTER);
		contentPane.add(mainBar, BorderLayout.NORTH);
		
		try{
		    // Open the file that is the first 
		    // command line parameter
		    FileInputStream fstream = new FileInputStream("toolbarlist.dat");
		    // Get the object of DataInputStream
		    DataInputStream in = new DataInputStream(fstream);
		    BufferedReader br = new BufferedReader(new InputStreamReader(in));
		    String strLine;
		    //Read File Line By Line
		    while ((strLine = br.readLine()) != null)
		    {
		        // Print the content on the console
		    	if (strLine.equals("")) continue;
		    	menuMaster.MenuBuilder otherBarBldr = new menuMaster.MenuBuilder(strLine, MenuBuilder.BUILDTOOLBAR);
		    	contentPane.add(otherBarBldr.getToolBar(),BorderLayout.SOUTH);
		    }
		    //Close the input stream
		    in.close();
		}
		catch (IOException e)
		{
			//Catch exception if any
		    System.err.println("Error: " + e.getMessage());
		}
		
		contentPane.add(canvas, BorderLayout.CENTER);
		
		
	    this.pack();
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.addWindowListener(new CloseHandler());				
		this.setJMenuBar(topMenu);
		this.setSize(850,650);
	}
	
	public void run()
	{
		this.setVisible(true);
	}
}